<%@ taglib prefix="page" uri="http://com.qipay/pagelib" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ page language="java" import="com.qipay.admin.utils.SiteHelper" contentType="text/html;charset=utf-8" %>
<%@ page import="com.qipay.baas.model.CpAccount" %>
<%@ page import="com.qipay.configuration.config.ConfigurationEnum" %>
<%@ page import="com.qipay.user.entity.UserLevel" %>
<%@ page import="org.apache.commons.collections.CollectionUtils" %>
<%@ page import="org.apache.commons.lang3.StringUtils" %>
<%@ page import="java.util.List" %>
<%@ page import="java.util.Map" %>
<%@ page import="com.qipay.admin.withdraw.vo.*" %>
<!doctype html>
<html>
<head>
    <jsp:include page="head.jsp"></jsp:include>
    <link rel="stylesheet" href="<%= request.getAttribute("StaticHost") %>/css/dataTables.bootstrap.min.css">
    <link rel="stylesheet" href="<%= request.getAttribute("StaticHost") %>/css/bootstrapValidator.min.css">
    <script src="<%= request.getAttribute("StaticHost") %>/js/jquery.js"></script>
    <script src="<%= request.getAttribute("StaticHost") %>/js/address.js"></script>
</head>
<body class="devpreview hold-transition skin-blue sidebar-mini">
<div class="wrapper">
    <jsp:include page="nav.jsp"></jsp:include>
    <jsp:include page="left.jsp"></jsp:include>
    <div class="content-wrapper">
        <section class="content">
            <div class="row">
                <div class="col-xs-12">
                    <div class="box box-info">
                        <div class="box-header with-border">
                            <h3 class="box-title">提现|代付 申请</h3>
                        </div>
                        <%
                            UserLevel userLevel = (UserLevel) request.getAttribute("level");
                        %>
                        <div class="box-body" id="dvContent">
                            <div class="box-body">
                                <div class="nav-tabs-custom col-sm-12 col-md-6">

                                    <div class="box-header with-border">
                                        <h3 class="box-title">类型选择</h3>
                                    </div>

                                    <ul id="operation_tab" class="nav nav-tabs">
                                        <li class="active" key="0"><a href="#tab_withdraw" data-toggle="tab"
                                                                      aria-expanded="true" style="color: #00a65a;">提现</a></li>
                                    </ul>

                                    <div class="tab-content">
                                        <div class="tab-pane active" id="tab_withdraw">
                                            <div class="form-group">
                                                <label for="cardBankList">绑定银行卡</label>
                                                <select id="cardBankList" class="form-control">
                                                    <%
                                                        List<BindVo> cardList = (List<BindVo>) request.getAttribute("cardList");
                                                        if (CollectionUtils.isNotEmpty(cardList)) {
                                                            for (BindVo p : cardList) {
                                                    %>
                                                    <option bank="<%=p.getCardBank()%>"
                                                            subBank="<%=p.getCardSubBank()%>"
                                                            no="<%=p.getCardNo()%>"
                                                            name="<%=p.getName()%>"
                                                            mobile="<%=p.getMobile()%>">
                                                        <%=p.getCardBank() + " - " + p.getCardSubBank() + " - " + p.getCardNo()%>
                                                    </option>
                                                    <%
                                                            }
                                                        }
                                                    %>
                                                </select>
                                            </div>
                                            <div class="form-group">
                                                <label for="withdraw_type">提取来源</label>
                                                <select id="withdraw_type" class="form-control type">
                                                    <option type="0">可用余额</option>
                                                    <%if (userLevel == UserLevel.CP) {%>
                                                    <option type="1">垫支余额</option>
                                                    <%}%>
                                                </select>
                                            </div>
                                        </div>
                                        <div class="tab-pane" id="tab_transfer">
                                            <div class="form-group">
                                                <label for="transfer_type">提取来源</label>
                                                <select id="transfer_type" class="form-control type">
                                                    <option type="0">可用余额</option>
                                                    <%if (userLevel == UserLevel.CP) {%>
                                                    <option type="1">垫支余额</option>
                                                    <%}%>
                                                </select>

                                            </div>
                                        </div>
                                    </div>
                                </div>
                                <div class="col-sm-12 col-md-6">
                                    <div class="box-header with-border">
                                        <h3 class="box-title">基础信息</h3>
                                    </div>
                                    <form class="form-horizontal">
                                        <label for="balance" class="col-sm-5 col-md-5 col-lg-5">账户总余额</label>
                                        <div class="form-group col-sm-7 col-md-7 col-lg-7"><input type="text"
                                                                                                  class="form-control"
                                                                                                  id="balance" readonly
                                                                                                  value="">
                                        </div>
                                        <label for="available" class="col-sm-5 col-md-5 col-lg-5">账户可提余额</label>
                                        <div class="form-group col-sm-7 col-md-7 col-lg-7"><input type="text"
                                                                                                  class="form-control"
                                                                                                  id="available"
                                                                                                  readonly
                                                                                                  value="">
                                        </div>
                                        <label for="disabled" class="col-sm-5 col-md-5 col-lg-5">账户不可提余额</label>
                                        <div class="form-group col-sm-7 col-md-7 col-lg-7"><input type="text"
                                                                                                  class="form-control"
                                                                                                  id="disabled" readonly
                                                                                                  value="">
                                            <input type="hidden" id="disabledVal"
                                                   value="">
                                        </div>
                                        <label hidden for="min" class="col-sm-5 col-md-5 col-lg-5">最小金额</label>
                                        <div hidden class="form-group col-sm-7 col-md-7 col-lg-7"><input type="text"
                                                                                                         class="form-control"
                                                                                                         id="min"
                                                                                                         readonly
                                                                                                         value="">
                                            <input type="hidden" id="minVal"
                                                   value="">
                                        </div>
                                        <label hidden for="max" class="col-sm-5 col-md-5 col-lg-5">最大金额</label>
                                        <div hidden class="form-group col-sm-7 col-md-7 col-lg-7"><input type="text"
                                                                                                         class="form-control"
                                                                                                         id="max"
                                                                                                         readonly
                                                                                                         value="">

                                        </div>
                                        <label hidden for="cost" class="col-sm-5 col-md-5 col-lg-5">可用余额-固定收费</label>
                                        <div hidden class="form-group col-sm-7 col-md-7 col-lg-7"><input type="text"
                                                                                                         class="form-control"
                                                                                                         id="cost"
                                                                                                         readonly
                                                                                                         value="">
                                        </div>

                                    </form>
                                </div>
                            </div>
                        </div>
                        <div class="box border" >
                            <div class="box-header with-border">
                                <h3 class="box-title">表单明细(<span style="color: red;">
                                    温馨提示：提现审核时间为00:30~24:00。

                                </span>)</h3>
                            </div>
                            <div class="box-body">
                                <form class="form-horizontal" action="/withdraw" method="POST" id="applyForm">
                                    <input type="hidden" class="form-control" id="id" name="id"
                                           value="">
                                    <input type="hidden" class="form-control" id="operation"
                                           name="operation" value="">
                                    <input type="hidden" class="form-control" id="type"
                                           name="type" value="">
                                    <label for="operationName" class="col-sm-2">操作</label>
                                    <div class="form-group col-sm-10"><input type="text" class="form-control"
                                                                             id="operationName" name="operationName"
                                                                             readonly
                                                                             value="">
                                    </div>
                                    <label class="col-sm-2" for="amount">金额</label>
                                    <div class="form-group col-sm-10"><input type="text" class="form-control"
                                                                             id="amount" name="amount" onchange="calc()"
                                                                             value="">
                                    </div>
                                    <label class="col-sm-2" for="fee">手续费</label>
                                    <div class="form-group col-sm-10"><input type="text" class="form-control"
                                                                             id="fee"
                                                                             name="fee" readonly
                                                                             value="">
                                    </div>
                                    <label class="col-sm-2" for="actualAmount">到账金额</label>
                                    <div class="form-group col-sm-10"><input type="text" class="form-control"
                                                                             readonly
                                                                             id="actualAmount"
                                                                             name="actualAmount"
                                                                             value="">
                                    </div>

                                    <label class="col-sm-2" >支行所在省</label>
                                    <div class="form-group col-sm-10">
                                        <select id="province" class="form-control" name="province">
                                        </select>
                                    </div>

                                    <label class="col-sm-2">支行所在市</label>
                                    <div class="form-group col-sm-10">
                                        <select id="city" class="form-control" onchange="findSunBank();" name="city">
                                        </select>
                                    </div>
                                    <div class="form-group col-sm-10" style="display: none">
                                        <select id="county"></select>
                                    </div>

                                    <label class="col-sm-2" >银行名称</label>
                                    <div class="form-group col-sm-10">
                                        <select id="banks" name="cardBank" onchange="findSunBank();" class="form-control">
                                            <option value="招商银行">招商银行</option>
                                            <option value="中国工商银行">中国工商银行</option>
                                            <option value="中国建设银行">中国建设银行</option>
                                            <option value="浦发银行">浦发银行</option>
                                            <option value="中国农业银行">中国农业银行</option>
                                            <option value="中国民生银行">中国民生银行</option>
                                            <option value="平安银行">平安银行</option>
                                            <option value="兴业银行">兴业银行</option>
                                            <option value="交通银行">交通银行</option>
                                            <option value="中信银行">中信银行</option>
                                            <option value="中国光大银行">中国光大银行</option>
                                            <option value="华夏银行">华夏银行</option>
                                            <option value="中国银行">中国银行</option>
                                            <option value="广发银行">广发银行</option>
                                            <option value="邮政储蓄银行">邮政储蓄银行</option>
                                            <option value="其他银行">其他银行</option>
                                        </select>
                                    </div>

                                    <label class="col-sm-2">支行名称</label>
                                    <div class="form-group col-sm-10">
                                        <%--<select id="cardSubBank" name="cardSubBank" class="form-control">
                                        </select>--%>
                                        <input style="display:inline-block;width:50%;" type="text" class="form-control"
                                               id="cardSubBank"
                                               name="cardSubBank"
                                               placeholder="支行名称"
                                               value=""><span style="color:red;font-size: 20px;">(请认真填写分行名称，填写错误会导致无法到账)</span>
                                    </div>
                                    <label class="col-sm-2" for="cardNo">银行卡号</label>
                                    <div class="form-group col-sm-10"><input type="text" class="form-control"
                                                                             id="cardNo"
                                                                             name="cardNo"
                                                                             placeholder="银行卡号"
                                                                             value="">
                                    </div>
                                    <label class="col-sm-2" for="name">开户名</label>
                                    <div class="form-group col-sm-10"><input type="text" class="form-control"
                                                                             id="name"
                                                                             name="name"
                                                                             placeholder="开户人姓名"
                                                                             value="">
                                    </div>
                                    <label class="col-sm-2" for="withdrawPassword">操作密码</label>
                                    <div class="form-group col-sm-10"><input type="text" class="form-control"
                                                                             id="withdrawPassword"
                                                                             name="withdrawPassword"
                                                                             value="">
                                    </div>


                                    <div class="box-footer">
                                        <button type="button" class="btn btn-default" onclick="window.history.back()">
                                            返回
                                        </button>
                                        <button type="submit" class="btn btn-primary" id="btnSubmit">提交</button>
                                    </div>
                                </form>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </section>

    </div>
    <%--<jsp:include page="foot.jsp"></jsp:include>--%>
</div>

<script src="<%= request.getAttribute("StaticHost") %>/js/adminlte.min.js"></script>
<script src="<%= request.getAttribute("StaticHost") %>/js/icheck.min.js"></script>
<script src="<%= request.getAttribute("StaticHost") %>/js/bootstrapValidator.min.js"></script>
<script src="<%= request.getAttribute("StaticHost") %>/js/business/withdraw_apply.js?v=<%= request.getAttribute("StaticVersion") %>"></script>
<script type="text/javascript">
    //查询银行
    function findSunBank() {
        var province = $("#province").val();
        var city = $("#city").val();
        var bank = $("#banks").val();
        //alert(province+":"+city+":"+bank);
        $.ajax({
            url: "${pageContext.request.contextPath}/ajaxBanks?province=" + province+"&city="+city+"&bank="+bank,
            type: "GET",
            async: true,//true异步 false同步
            context: this,
            timeout: 1000000,
            success: function (data) {
                var code0 = data.code;
                if (code0 != null && code0 == 200) {
                    var banks = data.data;
                    $('#cardSubBank').empty();
                    for(var i=0;i<banks.length;i++){
                        $('#cardSubBank').append('<option value="'+banks[i].sub_branch_name+'">'+banks[i].sub_branch_name+'</option>');
                        //alert($('#cities').find("option").length);
                    }
                    //alert(citys.length);

                }
                else {
                    $('#cardSubBank').empty();
                    //alert("未查询到相关支行数据");
                }
            },
            error: function (d, s, b) {
                alert("失败:" + b);
            }
        });


    }


    $(function () {

        // init.args
        var operation = $("#operation_tab li.active").attr("key");
        var password = $("#password").val();
        var min = Number($("#minVal").val());
        var max = Number($("#maxVal").val());
        console.log(min);
        console.log($("#maxVal").val() + "= " + max);
        var start = $("#start").val();
        var end = $("#end").val();
        var code = "";
        type = $(".tab-content div.active .type").find("option:selected").attr("type");

        // init.fill
        $("#type").val(type);
        $("#operation").val(operation);
        $("#operationName").val($("#operation_tab li.active a").text());
        initAmount();

        // init.validator
        $('#applyForm').bootstrapValidator({
            framework: 'bootstrap',
            icon: {
                valid: 'glyphicon glyphicon-ok',
                invalid: 'glyphicon glyphicon-remove',
                validating: 'glyphicon glyphicon-refresh'
            },
            fields: {
                operationName: {
                    validators: {
                        notEmpty: {
                            message: '操作类型不能为空'
                        }, callback: {
                            message: '提现申请时间为 ' + start + '-' + end + ',请至有效时间后刷新界面重试',
                            callback: function (value, validator) {
                                return checkTime();
                            }
                        }
                    }
                },
                amount: {
                    validators: {
                        notEmpty: {
                            message: '操作金额不能为空'
                        },
                        callback: {
                            message: '操作金额不足',
                            callback: function (value, validator) {

                                var available = Number($("#availableVal").val());
                                var advance = Number($("#advanceVal").val());
                                return value <= (0 == type ? available : advance);
                            }
                        },
                        between: {
                            min: min,
                            max: max,
                            message: '操作金额范围为' + min + '到' + max
                        }
                    }
                },
                fee: {
                    validators: {
                        notEmpty: {
                            message: '扣费金额不能为空'
                        }
                    }
                },
                actualAmount: {
                    validators: {
                        notEmpty: {
                            message: '实际到账金额不能为空'
                        }
                    }
                },
                cardBank: {
                    validators: {
                        notEmpty: {
                            message: '银行名称不能为空'
                        }
                    }
                },
                cardSubBank: {
                    validators: {
                        notEmpty: {
                            message: '支行名称不能为空'
                        }
                    }
                },
                cardNo: {
                    validators: {
                        notEmpty: {
                            message: '卡号不能为空'
                        }, stringLength: {
                            min: 16,
                            max: 19,
                            message: '银行卡位数有误'
                        },
                    }
                },
                idCardNo: {
                    validators: {
                        notEmpty: {
                            message: '卡号不能为空'
                        }, stringLength: {
                            min: 17,
                            max: 18,
                            message: '身份证位数有误'
                        },
                    }
                },
                name: {
                    validators: {
                        notEmpty: {
                            message: '姓名不能为空'
                        }
                    }
                },
                // validCode: {
                //     validators: {
                //         notEmpty: {
                //             message: '手机校验码不能为空'
                //         },
                //         callback: {
                //             message: '手机校验码错误',
                //             callback: function (value, validator) {
                //                 return code == value;
                //             }
                //         },
                //     }
                // },
                withdrawPassword: {
                    validators: {
                        notEmpty: {
                            message: '操作密码不能为空'
                        },
                        callback: {
                            message: '操作密码错误',
                            callback: function (value, validator) {
                                return password == value;
                            }
                        },
                    },
                    regexp: {
                        regexp: /^[0-9]{6}$/,
                        message: '操作密码位数有误'
                    }
                },
            }
        });

        // sms
        $("#sendValid").click(function () {
            //看是否在两分钟之内，有没有发送验证码，如果有，则拒绝发送---服务端查询。
            $.ajax({
                url: "${pageContext.request.contextPath}/withdraw/queryValid",
                type: "PUT",
                async: true,
                context: this,
                timeout: 50000,
                success: function (data) {
                    if (null != data && data.code == 200) {
                        var limit = 60;
                        $("#sendValid").attr("disabled", true);
                        var timer = setInterval(function () {
                            if (--limit != 0) {
                                console.log(limit)
                                $("#sendValid").text(limit + "秒后重发");
                            } else {
                                console.log("60s pass")
                                $("#sendValid").text("发送校验码");
                                $("#sendValid").attr("disabled", false);
                                clearInterval(timer);
                            }
                        }, 1000);
                        // request
                        $.get("/withdraw/valid", function (data) {
                            if (null != data && data.code == 200) {
                                code = data.data
                            } else {
                                console.log("get fail")
                                clearInterval(timer);
                                $("#sendValid").attr("disabled", false);
                                $("#sendValid").text("发送校验码");
                                alert("失败:" + data.msg);
                            }
                        }).fail(function () {
                            console.log("get request fail")
                            clearInterval(timer);
                            $("#sendValid").attr("disabled", false);
                            $("#sendValid").text("发送校验码");
                        });
                    } else {
                        alert("操作过于频繁，提现操作限定为2分钟一次噢");
                    }
                },
                error: function (d, s, b) {
                    //alert("失败:" + b);
                    $('#banlance').val("0");
                }
            });


        });
    });

    function checkTime() {
        // curTime
        var date = new Date();
        var start = $("#start").val();
        var end = $("#end").val();
        var curTime = date.getHours() * 100 + date.getMinutes();
        var startLimit = parseFloat(start.replace(":", ""));
        var endLimit = parseFloat(end.replace(":", ""));
        // check
        return curTime > startLimit && curTime < endLimit
    }

</script>
</body>
</html>

